package cn.alpha.common.springmvc;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import cn.alpha.common.Constants;
import cn.ffcs.system.po.Org;
import cn.ffcs.system.po.User;


public class CustomSessionArgumentResolver implements HandlerMethodArgumentResolver {
	
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.getParameterAnnotation(CustomSessionArgument.class) != null;
	}

	public Object resolveArgument(MethodParameter parameter,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		
		Class<?> paramClass=parameter.getParameterType();
		if(User.class.equals(paramClass))
			return  webRequest.getAttribute(Constants.SESSION_USER, WebRequest.SCOPE_SESSION);
		else if(Org.class.equals(paramClass))
			return webRequest.getAttribute(Constants.SESSION_ORG, WebRequest.SCOPE_SESSION);
		else 
			return null;
	}
}
